package com.artisan.domain.service;

import com.artisan.domain.dto.AdDto;
import com.artisan.domain.dto.PageDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.entity.MStartPage;
import com.artisan.domain.info.AdInfo;
import com.artisan.domain.repositoryInterface.IStartPageRepository;
import com.artisan.domain.serviceInterface.IStartPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 启动页业务类
 */
@Service
public class StartPageService implements IStartPageService {
    @Autowired
    private IStartPageRepository startPageRepository;

    @Override
    public PageDto<StartPageDto> findList(int pageIndex, int pageSize) {

        Pageable pageable = new QPageRequest(pageIndex, pageSize);
        Page<MStartPage> page = this.startPageRepository.findList(pageable);

        List<StartPageDto> dtos = new ArrayList<>();
        page.getContent().forEach(model -> {
            StartPageDto dto = new StartPageDto();
            dto.setId(model.getId());
            dto.setNumber(model.getNumber());
            dto.setName(model.getName());
            dto.setStartDate(model.getStartDate());
            dto.setEndDate(model.getEndDate());
            dto.setPosition(model.getPosition());
            dto.setStayTime(model.getStayTime());
            dto.setAddDate(model.getAddDate());
            dto.setStatus(model.getStatus());

            dtos.add(dto);
        });

        PageDto<StartPageDto> result = new PageDto();
        result.setContent(dtos);
        result.setNumber(page.getNumber());
        result.setSize(page.getSize());
        result.setTotalElements(page.getTotalElements());

        return result;
    }

    @Override
    public int add(StartPageDto dto) {
        MStartPage model = new MStartPage();
        model.setNumber(dto.getNumber());
        model.setName(dto.getName());
        model.setStartDate(dto.getStartDate());
        model.setEndDate(dto.getEndDate());
        model.setPosition(dto.getPosition());
        model.setStayTime(dto.getStayTime());
        model.setAddDate(System.currentTimeMillis());
        model.setStatus(1);
        model.setLink(dto.getLink());
        model.setContent(dto.getContent());

        model = this.startPageRepository.saveAndFlush(model);

        return model.getId();

    }

    @Override
    public boolean delete(int id) {
        this.startPageRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean update(StartPageDto dto) {
        MStartPage model = this.startPageRepository.findById(dto.getId()).get();
        model.setNumber(dto.getNumber());
        model.setName(dto.getName());
        model.setStartDate(dto.getStartDate());
        model.setEndDate(dto.getEndDate());
        model.setPosition(dto.getPosition());
        model.setStayTime(dto.getStayTime());
        model.setAddDate(System.currentTimeMillis());
        model.setStatus(1);
        model.setLink(dto.getLink());
        model.setContent(dto.getContent());

        model = this.startPageRepository.saveAndFlush(model);

        return model.getId() > 0;
    }

    @Override
    public boolean updateStatus(int id, int status) {
        MStartPage model = this.startPageRepository.findById(id).get();
        model.setStatus(status);

        model = this.startPageRepository.saveAndFlush(model);

        return model.getId() > 0;
    }
}
